﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.byhand
{
    public class BestB2B : Information
    {
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("userType", "corp");
            pd.Add("validateCode", vcode);
            pd.Add("Submit", "登录");
            pd.Add("PersistantLogin", "1");
            string html = source.Post("http://www.bestb2b.com/signin.aspx", pd.Read(), "http://www.bestb2b.com/jsp/signin.jsp");
            if (html.IndexOf("href=\"/base/signout.jsp\">退出</a>") != -1)
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            string html = source.Get("http://www.bestb2b.com/editbusiness.aspx");
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据Regex.Match(html,"").Groups[1].Value
            sfd.Write("org.apache.struts.taglib.html.TOKEN", Regex.Match(html, "name=\"org.apache.struts.taglib.html.TOKEN\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("PAGE_INDEX", Regex.Match(html, "name=\"PAGE_INDEX\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("action", "submit");
            sfd.Write("load_action", "new");
            sfd.Write("coming", "manageselloffers");
            sfd.Write("business.id", "0");
            sfd.Write("business.infoType", "供应信息");
            sfd.Write("strCreateTime", "");
            sfd.Write("strModifyTime", "");
            sfd.Write("clientID", Regex.Match(html, "name=\"clientID\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("business.isHidden", "");
            sfd.Write("business.corpId", Regex.Match(html, "name=\"business.corpId\" value=\"([^\"]+)\"").Groups[1].Value);
            sfd.Write("business.infoTitle", title);
            sfd.Write("business.keyword", ktag.Get(3, "，"));
            sfd.Write("business.bigIndustry", site.TypeId[0]);
            sfd.Write("business.smallIndustry", site.TypeId[1]);
            sfd.Write("business.selfindustry", "0");
            sfd.Write("business.commendStatus", "普通");
            sfd.Write("business.infoContent", content);
            sfd.Write("business.productType", "");
            sfd.Write("business.productPrice", "");
            sfd.Write("business.productCount", "");
            sfd.Write("business.issueDays", "360");
            //发送文件
            sfd.WriteFile("file0", RandomPicture.Get());
            //尾部数据
            sfd.Write("validateCode", vcode);
            //结尾
            html = source.Post("http://www.bestb2b.com/editbusiness.aspx", sfd.GetContentType(), sfd.Read(), "http://www.bestb2b.com/editbusiness.aspx");
            if (Regex.IsMatch(html, "<a href='(/business_[^']+)' target=\"_blank\">\\s*" + title + "\\s*</a>"))
            {
                info.Code = 1;
                info.Content = "http://www.bestb2b.com" + Regex.Match(html, "<a href='(/business_[^']+)' target=\"_blank\">\\s*" + title + "\\s*</a>").Groups[1].Value;
            }
            else if (Regex.IsMatch(html, "class=\"error\">.+?</div>"))
            {
                string result = MyFunction.ToText(Regex.Match(html, "class=\"error\">(.+?)</div>").Groups[1].Value);
                if (result.IndexOf("供求信息已经达到当天限制条数") != -1)
                {
                    info.Code = 3;
                }
                else
                {
                    info.Content = result;
                }
            }
            return info;
        }

        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://www.bestb2b.com/mynosiseimage");
        }
    }
}
